﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

using MSAP.Framework.Interfaces;
using MSAP.Framework.Controls;

namespace MSAP.Framework
{
    public class ServicePageBase : IServicePage
    {
        #region IServicePage Members

        private ServicePageBase() 
        {  
            // Pas d'instance sans parametre 
        }

        protected ServicePageBase(string id, string title, Uri serviceUri, string comments, ImageSource icon)
        {
            this.m_ID = id;
            this.m_Title = title;
            this.m_ServiceUri = serviceUri;
            this.m_Comments = comments;
            this.m_Icon = icon;
            this.m_ServiceActions = new List<IServiceAction>();
        }

        protected ServicePageBase(string id, string title, Uri serviceUri, string comments)
        {
            this.m_ID = id;
            this.m_Title = title;
            this.m_ServiceUri = serviceUri;
            this.m_Comments = comments;
            this.m_Icon = null;
            this.m_ServiceActions = new List<IServiceAction>();
        }

        public virtual void Init(IModule module)
        {
            m_Module = module;
        }
        
        public virtual IModule Module
        {
            get { return m_Module; }
        }
        
        public IShell Shell
        {
            get { return m_Module.Shell; }
        }

        public string ID
        {
            get { return m_ID; }
        }

        public string Title
        {
            get { return m_Title; }
        }

        public Uri ServiceUri
        {
            get { return m_ServiceUri; }
        }

        public string Comments
        {
            get { return m_Comments; }
        }

        public ImageSource Icon
        {
            get { return m_Icon; }
        }

        public List<IServiceAction> ServiceActions
        {
            get { return m_ServiceActions; }
        }

        public TabItem View
        {
            get
            {
                return m_View;
            }
            set
            {
                AnimatedTabItem item = value as AnimatedTabItem;
                if (item != null)
                    item.CloseCommand = CloseViewCommand;
                m_View = item;
            }
        }

        public virtual ICommand CloseViewCommand
        {
            get
            {
                if (m_CloseViewCommand == null)
                {
                    m_CloseViewCommand = new RelayCommand(Execute => CloseView(), CanExecute => CanCloseView());
                }
                return m_CloseViewCommand;
            }
        }
        
        #endregion

        protected virtual void CloseView()
        {
            Module.RemoveServicePage(this);
        }

        protected virtual bool CanCloseView()
        {
            return Module.CanRemoveServicePage(this);
        }

        #region INotifyPropertyChanged Members

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region private fields
        private IModule m_Module;
        private string m_ID;
        private string m_Title;
        private Uri m_ServiceUri;
        private string m_Comments;
        private ImageSource m_Icon;
        private List<IServiceAction> m_ServiceActions;
        private TabItem m_View;
        private ICommand m_CloseViewCommand;
        #endregion
    }
}
